Imports System
Imports System.Text
Imports System.Data
Imports System.Collections.Generic

Imports Framework

#Region "Enum: Primary Key Values"
'<CLSCompliant(True)> _
'Public Enum ETemplate
'    Huey = 1
'    Duey = 2
'    Louie = 3
'End Enum
#End Region

'Table-Row Class (Customisable half)
Partial Public Class CTemplate

#Region "Constants"
#End Region

#Region "Constructors (Public)"
    'Default Connection String
    Public Sub New()
        MyBase.New()
    End Sub
    
    'Alternative Connection String
    Public Sub New(ByVal dataSrc as CDataSrc)
        MyBase.New(dataSrc)
    End Sub
    
    'Hidden (UI code should use cache instead)
    Protected Friend Sub New([templateId] As Integer)
        MyBase.New([templateId])
    End Sub
    Protected Friend Sub New(ByVal dataSrc As CDataSrc, [templateId] As Integer)
        MyBase.New(dataSrc, [templateId])
    End Sub
    Protected Friend Sub New(ByVal dataSrc As CDataSrc, ByVal [templateId] As Integer, ByVal txOrNull As IDbTransaction)
        MyBase.New(dataSrc, [templateId], txOrNull)
    End Sub
#End Region

#Region "Default Values"
	Protected Overrides Sub InitValues_Custom()
		'm_sampleDateCreated = DateTime.Now
		'm_sampleSortOrder   = 0
	End Sub
#End Region

#Region "Default Connection String"
	Protected Overrides Function DefaultDataSrc() As CDataSrc
        Return CDataSrc.Default
    End Function
#End Region

#Region "Properties - Relationships"
	'Relationships - Foriegn Keys (e.g parent)
	Public ReadOnly Property [Contents] As CContentList
		Get
			Return CContent.Cache.GetByTemplateId(Me.TemplateId)
		End Get
	End Property
	Protected Function Contents_(ByVal tx As IDbTransaction) As CContentList   'Only used for cascade deletes
		With New CContent(DataSrc)
			Return .SelectByTemplateId(Me.TemplateId, tx)
		End With
	End Function

	'Relationships - Collections (e.g. children)


	'Relationships - 2-Step Walk
	Public ReadOnly Property [TemplateSets]() As CTemplateSetList
		Get
			Return Contents.TemplateSets 'There is also an id-only version of this property
		End Get
	End Property
	Public Function RemainingTemplateSets(ByVal search As String) As CTemplateSetList
		Return Contents.RemainingTemplateSets(search)
	End Function

	Public ReadOnly Property TemplateSetCount As Integer
		Get
			Return TemplateSets.Count
		End Get
	End Property
	Public ReadOnly Property NameAndCount As String
		Get
			Return CUtilities.NameAndCount(TemplateName, TemplateSets)
		End Get
	End Property
#End Region

#Region "Properties - Customisation"
	'Derived/ReadOnly (e.g. xml classes, presentation logic)
#End Region

#Region "Save/Delete Overrides"
	Public Overrides Sub Save(txOrNull As IDbTransaction)
		If m_insertPending Then
			TemplateName = CTemplateList.Normalise(TemplateName)
			m_templateId = TemplateName.GetHashCode()
		End If
		MyBase.Save(txOrNull)
	End Sub
	Public Overrides Sub Delete(ByVal txOrNull As IDbTransaction)
		'Use a transaction if none supplied
		If txOrNull Is Nothing Then
			BulkDelete(Me)
			Exit Sub
		End If

		'Cascade-Delete (all child collections)
		Me.Contents_(txOrNull).DeleteAll(txOrNull)

		'Normal Delete
		MyBase.Delete(txOrNull)
	End Sub
#End Region

#Region "Custom Database Queries"
	'(Not normally required for cached classes, use list class for searching etc)
	'For Stored Procs can use: MakeListTyped (matching schema), or DataSrc.ExecuteDataset (reports etc)
	'For Dynamic sql, can use: SelectSum, SelectDistinct, SelectCount, SelectWhere (inherited methods)
#End Region

#Region "Searching (Optional)"
	'For cached classes, custom seach logic resides in static methods on the list class
	' e.g. CTemplate.Cache.Search("..")

	'See also the auto-generated methods based on indexes
	' e.g. CTemplate.Cache.GetBy...
#End Region

#Region "Caching Details"
	'Cache Key
	Friend Shared CACHE_KEY As String = GetType(CTemplate).ToString 'TABLE_NAME

    'Cache data
    Private Shared Function LoadCache() As CTemplateList
        Return New CTemplate().SelectAll()
    End Function
    
    'Cache Timeout
    Private Shared Sub SetCache(ByVal value As CTemplateList)
        If Not IsNothing(value) Then value.Sort()
        CCache.Set(CACHE_KEY, value) 'Optional parameter can override timeout (otherwise uses config-settings, which default to 3hrs)
    End Sub
    
    'Helper Method
    Private Function CacheGetById(ByVal list As CTemplateList) As CTemplate
        Return list.GetById(Me.TemplateId)
    End Function
#End Region

#Region "Cloning"
    Public Function Clone(ByVal target as CDataSrc, ByVal txOrNull as IDbTransaction) As CTemplate ', parentId As Integer) As CTemplate
        'Shallow copy: Copies the immediate record, excluding autogenerated Pks
        Dim copy As New CTemplate(Me, target)

        'Deep Copy - Child Entities: Cloned children must reference their cloned parent
        'copy.SampleParentId = parentId

        copy.Save(txOrNull)

        'Deep Copy - Parent Entities: Cloned parents also clone their child collections
        'Me.Children.Clone(target, txOrNull, copy.TemplateId)

        Return copy
    End Function
#End Region

#Region "ToXml"
    Protected Overrides Sub ToXml_Custom(ByVal w As System.Xml.XmlWriter)
        'Store(w, "Example", Me.Example)
    End Sub
#End Region

End Class
